//
//  YBAddCustomGroupViewController.m
//  GongXinDangJian
//
//  Created by apple on 17/12/5.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBAddCustomGroupViewController.h"

#import "YBCustomGroupHeadView.h"
#define HEADID @"YBCustomGroupHeadView"

#import "YBCustomGroupTableViewCell.h"
#define CELLID @"YBCustomGroupTableViewCell"

#import "YBCreatCustomGroupViewController.h"

@interface YBAddCustomGroupViewController ()<UITableViewDelegate , UITableViewDataSource , YBCustomGroupHeadViewDelegate , YBPopupMenuDelegate>
@property (nonatomic , strong) UITableView * tableView;

@property (nonatomic , strong) UIButton * creatButton;

@property (nonatomic , strong) NSArray * deptArray;

@property (nonatomic , strong) NSArray * userArray;

@property (nonatomic , assign) BOOL isFirstIn;

@property (nonatomic , assign) NSInteger currentSection;
@end

@implementation YBAddCustomGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isFirstIn = YES;
    self.titlelabel.text = @"自定义分组";
    self.currentSection = 0;
    [self ybInitUserInterface];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.isFirstIn) {
        [YJProgressHUD showMessage:@"加载中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
    }
    [self ybInitCustomGroupView];
}


-(void)ybInitUserInterface{
    self.view.backgroundColor = [UIColor themeGrayColor];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.creatButton];
}


-(void)ybInitCustomGroupView{
    [YBRequestManager request_customGroupListWithSuccessBlock:^(id object) {
        if (self.isFirstIn) {
            [YJProgressHUD hide];
            self.isFirstIn = NO;
        }
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray * tempUserArray = [NSArray yy_modelArrayWithClass:[YBPeopleInfoModel class] json:object[@"ORG_MYSELF_DEPT_MEMBERS"]];
            NSArray * tempDeptArray = [NSArray yy_modelArrayWithClass:[YBSelectHeadModel class] json:object[@"ORG_MYSELF_DEPTS"]];
            
            NSMutableArray * oftenTotalUserArray = [NSMutableArray array];
            for (YBSelectHeadModel * model in tempDeptArray) {
                model.ISOPEN = NO;
                
                for (YBSelectHeadModel * deptModel in self.deptArray) {
                    if ([deptModel.ID isEqualToString:model.ID]) {
                        model.ISOPEN = deptModel.ISOPEN;
                    }
                }
                NSMutableArray * tempArray = [NSMutableArray array];
                for (YBPeopleInfoModel * peopleModel in tempUserArray) {
                    if ([model.ID isEqualToString:peopleModel.FID]) {
                        [tempArray addObject:peopleModel];
                    }
                }
                [oftenTotalUserArray addObject:tempArray];
            }
            self.deptArray = tempDeptArray;
            self.userArray = [oftenTotalUserArray copy];
            [self.tableView reloadData];
        }
    } failBlock:^(id object) {
        if (self.isFirstIn) {
            [YJProgressHUD hide];
            self.isFirstIn = NO;
        }
        [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
    }];
}


-(void)option_creatButton:(UIButton *)sender{
    YBCreatCustomGroupViewController * creatVC = [[YBCreatCustomGroupViewController alloc]init];
    creatVC.title = @"创建分组";
    [self.navigationController pushViewController:creatVC animated:YES];
}


-(void)option_selectButton:(UIButton *)sender{
    NSInteger index = sender.tag - 1000;
    self.currentSection = index;
    CGPoint ponit = [sender convertPoint:CGPointMake(ItemWidth * 48, ItemWidth * 48) toView:[UIApplication sharedApplication].keyWindow];
    [YBPopupMenu showAtPoint:ponit titles:@[@"修改" , @"删除"] icons:@[] menuWidth:ItemWidth * 180 showCount:2 delegate:self];
    
}


- (void)ybPopupMenuDidSelectedAtIndex:(NSInteger)index ybPopupMenu:(YBPopupMenu *)ybPopupMenu{
    if (index == 0) {
        YBCreatCustomGroupViewController * creatVC = [[YBCreatCustomGroupViewController alloc]init];
        creatVC.title = @"修改分组";
        creatVC.isChange = YES;
        creatVC.model = self.deptArray[self.currentSection];
        creatVC.transformationArray = self.userArray[self.currentSection];
        [self.navigationController pushViewController:creatVC animated:YES];
    }
    else{
        YBSelectHeadModel * model = self.deptArray[self.currentSection];
        [YJProgressHUD showMessage:@"删除中..." duration:10 view:[UIApplication sharedApplication].keyWindow];
        [YBRequestManager request_deleteCustomGroupWithDeptId:model.ID successBlock:^(id object) {
            NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
            if ([model.STATE isEqualToString:@"20000"]) {
                self.isFirstIn = YES;
                [self ybInitCustomGroupView];
            }
            else{
                [YJProgressHUD hide];
                [YJProgressHUD showSuccess:@"删除失败" inview:self.view];
            }
        } failBlock:^(id object) {
            [YJProgressHUD hide];
            [YJProgressHUD showSuccess:@"网络错误" inview:self.view];
        }];
    }
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.deptArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YBSelectHeadModel * tempModel = self.deptArray[section];
    if (tempModel.ISOPEN) {
        NSArray * tempArray = self.userArray[section];
        return tempArray.count;
    }
    else{
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ItemHeight * 80;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YBCustomGroupTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.userArray[indexPath.section][indexPath.row];
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return ItemHeight * 80;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YBCustomGroupHeadView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HEADID];
    headerView.ybSection = section;
    YBSelectHeadModel * model = self.deptArray[section];
    NSArray * tempArray = self.userArray[section];
    model.PEOPLECOUNT = tempArray.count;
    headerView.model = model;
    headerView.delegate = self;
    headerView.selectButton.tag = 1000 + section;
    [headerView.selectButton addTarget:self action:@selector(option_selectButton:) forControlEvents:UIControlEventTouchUpInside];
    return headerView;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}


-(void)ybCustomGroupHeadViewDidClickWithSection:(NSInteger)section isOpen:(BOOL)isOpen
{
    ((YBSelectHeadModel *)(self.deptArray[section])).ISOPEN = isOpen;
    [self.tableView reloadData];
}


-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, navHeight + ItemHeight * 20, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - ItemHeight * 120 - navHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[YBCustomGroupTableViewCell class] forCellReuseIdentifier:CELLID];
        [_tableView registerClass:[YBCustomGroupHeadView class] forHeaderFooterViewReuseIdentifier:HEADID];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}


-(UIButton *)creatButton
{
    if (!_creatButton) {
        _creatButton = [ToolClass createButtonWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] - ItemHeight * 100, [UIScreen zj_screenWith], ItemHeight * 100) text:@"创建分组" textColor:[UIColor whiteColor] imageName:@"add_people" font:16 imageSize:ItemWidth * 42];
        _creatButton.backgroundColor = [UIColor themeColor];
        [_creatButton addTarget:self action:@selector(option_creatButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _creatButton;
}

@end
